var numi = 1;
let pages = 0;
let page = 0;
var fenyearr = "";
var Department = JSON.parse(localStorage.getItem("Department"));
var employees = JSON.parse(localStorage.getItem("employees"));
$(document).ready(function(){
    initialize(employees);
})
$(".sear").click(function (e) { 
    e.preventDefault();
    let str = $(".sear_tiao").val();
    page = 0;
    searDep(str);
});
$(".bu").change(function (e) { 
    e.preventDefault();
    let str = $(".bu").val();
    page = 0;
    searDep1(str);
});
$("#addq").click(function (e) { 
    e.preventDefault();
    let emnum = $(".emnum").val();
    let emna = $(".emna").val();
    let emtel = $(".emtel").val();
    let emz = $(".emz").val();
    let depid = $(".depid").val();
    var result = Department.findIndex((v) => {
        return v.DepName === depid;
    });
    depid = Department[result].DepID;
    if(emnum == "" ||emna == "") {

    }else {
        addDep(emnum,emna,emtel,emz,depid,employees);
    }
    $("#depAdd").modal('hide');
    $(".emnum").val("");
    $(".emna").val("");
    $(".emtel").val(""); 
    $(".emz").val("");
});
$("#delq").click(function (e) { 
    e.preventDefault();
    delDep(numi);
});
$("#upq").click(function (e) { 
    e.preventDefault();
    if($(".emnum1").val() !="") {

        employees[numi].role = $(".emnum1").val();
        employees[numi].Name = $(".emna1").val();
        employees[numi].Tel = $(".emtel1").val();
        employees[numi].Duty = $(".emz1").val();
        var result = Department.findIndex((v) => {
            return v.DepName === $(".upbu").val();
        });
        employees[numi].DepID = Department[result].DepID;
        console.log(Department);
        $("#depUp").modal('hide');
        $(".dename").val("");
        $(".dere").val("");
        $(".emnum1").val("");
        $(".emna1").val("");
        $(".emtel1").val("");
        $(".emz1").val("");
        localStorage.setItem("employees", JSON.stringify(employees));
        initia(employees);
    }else {
        $("#depUp").modal('hide');
        initia(employees);
    }
    
});
$(".juzhong a").click(function (e) { 
    e.preventDefault();
    num = $(this).parent().val();
    if(page != 1) {
        if(num==98) {
            page--;
            initia(fenyearr);
        }
    }
    if(page != pages) {
        if(num==99) {
            page++
            initia(fenyearr);
        }
    }
});

$('#depDel').on('show.bs.modal', function (event) {
    let numid = $($(event.relatedTarget).parent().parent().siblings()).html();
    console.log(numid);
    var result = employees.findIndex((v) => {
        return v.role === numid;
    });
    numi = result;
    $(".emnum").html(employees[result].role);
    $(".emname").html(employees[result].Name);
})
$('#depUp').on('show.bs.modal', function (event) {
    let numid = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = employees.findIndex((v) => {
        return v.role === numid;
    });
    numi = result;
    // console.log(result);
    $(".emnum1").attr("placeholder",employees[result].role);
    $(".emna1").attr("placeholder",employees[result].Name);
    $(".emtel1").attr("placeholder",employees[result].Tel);
    $(".emz1").attr("placeholder",employees[result].Duty);
})
$('#depShow').on('show.bs.modal', function (event) {
    let numid = $($(event.relatedTarget).parent().parent().siblings()).html();
    var result = employees.findIndex((v) => {
        return v.role === numid;
    });
    $(".emnum2").html(employees[result].role);
    $(".emname2").html(employees[result].Name);
    $(".emema2").html(employees[result].Email);
    $(".emaddr2").html(employees[result].Addr);
})



//初始化加载
function initialize(arr1) {
    fenyearr = arr1;
    for (let i = 0; i < Department.length; i++) {
        let str = `<option>${Department[i].DepName}</option>`;
        $(".bu").append(str);
        $(".bu1").append(str);
    }
   

    let arr = fenye(arr1);
    for(let i = 0; i<arr.length; i++) {
        let str1 = `<tr>
                        <td>${arr[i].role}</td>
                        <td>${arr[i].Name}</td>
                        <td>${arr[i].Tel}</td>
                        <td>${arr[i].Duty}</td>
                        <td>${arr[i].times}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        $(".dep_table").append(str1);
    }
}
//二次加载
function initia(arr1) {
    fenyearr = arr1;
    $(".biaotou").siblings().remove();
    let arr = fenye(arr1).concat();

    for(let i = 0; i<arr.length; i++) {
        let str1 = `<tr>
                        <td>${arr[i].role}</td>
                        <td>${arr[i].Name}</td>
                        <td>${arr[i].Tel}</td>
                        <td>${arr[i].Duty}</td>
                        <td>${arr[i].times}</td>
                        <td>
                            <div class="btn-group" role="group" aria-label="...">
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depDel" id="del1">删除</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depUp" id="cup">修改</button>
                                <button type="button" class="btn btn-default" data-toggle="modal" data-target="#depShow" id="ck">查看</button>
                            </div>
                        </td>
                    </tr>`;
        $(".dep_table").append(str1);
    }
}
//新增
function addDep(emnum,emna,emtel,emz,depid,arr){
    let timedate = new Date;
    let str = `${timedate.getFullYear()}-${timedate.getMonth()+1}-${timedate.getDate()}`;
    var add = {
        id: arr.length+1,
        Name: `${emna}`,
        Age: "未填",
        Sex: "未填",
        Tel: parseInt(emtel),
        Duty: `${emz}`,
        role: `${emnum}`,
        DepID: depid,
        Email: "未填",
        Account: parseInt(emtel),
        PassWord: 123456,
        State: "未填",
        times: str,
        quanid: 2,
        quan: "员工",
        Addr: "未填",
        Url: "images/1.png"
    }
    console.log(add);
    arr.push(add);
    localStorage.setItem("employees",JSON.stringify(arr));
    employees = JSON.parse(localStorage.getItem("employees"));
    initia(employees);
    // location.reload();
}
//删除
function delDep(numi) {
    employees.splice(numi,1);
    localStorage.setItem("employees", JSON.stringify(employees));
    employees = JSON.parse(localStorage.getItem("employees"));
    $("#depDel").modal('hide');
    initia(employees);
}
//修改
function upDep() {

}
//查看
function showDep() {

}
//搜索
function searDep(str) {
    let arr1 = [];
    employees = JSON.parse(localStorage.getItem("employees"));
    if(str != "") {
        var result = employees.findIndex((v) => {
            return v.Name === str;
        });
        if(result == -1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        } else {
            arr1.push(employees[result]);
            initia(arr1);
        }
    }else {
        if(employees.length == $(".dep_table tbody").children().length-1) {
            $('#tis').popover('show');
            setTimeout(function(){
                $('#tis').popover('hide');
            },600)
        }else {
            location.reload();
        }
    }
}
function searDep1(str) {
    let arr1 = [];
    employees = JSON.parse(localStorage.getItem("employees"));
    if(str == "全部") {
        location.reload();
    }else {
        var result = Department.findIndex((v) => {
            return v.DepName === str;
        });
        for(let i = 0; i<employees.length; i++) {
            if(employees[i].DepID == Department[result].DepID){
                arr1.push(employees[i]);
            }
        }
        // console.log(arr1);
        initia(arr1);
    }
}
function fenye (arr) {
    let xia  = $(".xia").clone(true);
    $(".shang").nextAll().remove();
    $(".shang").after(xia);
    let num = 0;
    if(arr.length<=6) {
        pages = 1
        num = arr.length;
    }else {
        pages = parseInt(arr.length/6);
        num = 6;
        if(arr.length%6 != 0) {
            pages += 1;
            
        }
    }
    for (let i = 1; i <= pages; i++) {
        $(".xia").before(`<li class= ab,a${i} value = ${i}><a onclick = "dijifenye(this)">${i}</a></li>`);
    }
    if(page == 0) {
        $(".a1").addClass("active");
        page=1;
    }else {
        $(`.a${page}`).addClass("active");
        
    }
    if(page == 1) {
        $(".shang").addClass("disabled");
    }else {
        $(".shang").removeClass("disabled")
    }
    if(page == pages) {
        $(".xia").addClass("disabled");
    }else {
        $(".xia").removeClass("disabled")
    }
    let arr1 = arr.concat();

    return arr1.splice(page*6-6,num);
}
function dijifenye(num) {
    page = parseInt($(num).html());
    console.log(parseInt($(num).html()));
    initia(fenyearr);
}






